package org.example.lotterysystem.controller;

import org.example.lotterysystem.common.errorcode.ControllerErrorCodeConstants;
import org.example.lotterysystem.common.exception.ControllerException;
import org.example.lotterysystem.common.pojo.CommonResult;
import org.example.lotterysystem.common.utils.JacksonUtil;
import org.example.lotterysystem.controller.param.CreateActivityParam;
import org.example.lotterysystem.controller.param.PageParam;
import org.example.lotterysystem.controller.result.CreateActivityResult;
import org.example.lotterysystem.controller.result.FindActivityListResult;
import org.example.lotterysystem.controller.result.GetActivityDetailResult;
import org.example.lotterysystem.service.ActivityService;
import org.example.lotterysystem.service.dto.ActivityDTO;
import org.example.lotterysystem.service.dto.ActivityDetailDTO;
import org.example.lotterysystem.service.dto.CreateActivityDTO;
import org.example.lotterysystem.service.dto.PageListDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.stream.Collectors;

@RestController
public class ActivityController {
    @Autowired
    private ActivityService activityService;

    private static final Logger logger= LoggerFactory.getLogger(ActivityController.class);

    /**
     * 创建活动
     * @param param
     * @return
     */
    @RequestMapping("/activity/create")
    public CommonResult<CreateActivityResult> createActivity(CreateActivityParam param){
        logger.info("createActivity CreateActivityParam:{}", JacksonUtil.writeValueAsString(param));
        return CommonResult.success(convertTOCreateActivityResult(activityService.createActivity(param)));
    }



    /**
     * 查找活动列表
     * @param param
     * @return
     */
    @RequestMapping("/activity/find_list")
    public CommonResult<FindActivityListResult> findActivityList(PageParam param){
        logger.info("findActivityList PageParam:{}",JacksonUtil.writeValueAsString(param));
        PageListDTO<ActivityDTO> activityList = activityService.findActivityList(param);
        convertTOFindActivityResult(activityList);
        return CommonResult.success(convertTOFindActivityResult(activityList));

    }
    @RequestMapping("/activity_detail/find")
    public CommonResult<GetActivityDetailResult>getActivityDetail(Long activityId){
        logger.info("getActivityDetail activityId：{}",activityId);
        ActivityDetailDTO activityDetailDTO=activityService.getActivityDetail(activityId);
        convertToGetActivityDetailResult(activityDetailDTO);
        return null;
    }

    private void convertToGetActivityDetailResult(ActivityDetailDTO activityDetailDTO) {
        if(activityDetailDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.GET_ACTIVITY_DETAIL_ERROR);
        }
        GetActivityDetailResult result=new GetActivityDetailResult();
        result.setActivityId(activityDetailDTO.getActivityId());
        result.setActivityName(activityDetailDTO.getActivityName());
        result.setDescription(activityDetailDTO.getDesc());
        result.setValid(activityDetailDTO.valid());
        result.setPrizes(activityDetailDTO.getPrizeDTOList()
                .stream().
                sorted(Comparator.comparingInt(prizeDTO-> prizeDTO.getTiers().getCode())).map(prizeDTO->{
                    GetActivityDetailResult.Prize prize=new GetActivityDetailResult.Prize();
                    prize.setPrizeId(prizeDTO.getPrizeId());
                    prize.setName(prizeDTO.getName());
                    prize.setImageUrl(prizeDTO.getImageUrl());
                    prize.setPrice(prizeDTO.getPrice());
                    prize.setDescription(prizeDTO.getDescription());
                    prize.setPrizeTierName(prizeDTO.getTiers().getMessage());
                    prize.setPrizeAmount(prizeDTO.getPrizeAmount());
                    prize.setValid(prizeDTO.valid());
                    return prize;
                }).collect(Collectors.toList())
        );
        result.setUsers(activityDetailDTO.getUserDTOList().stream().map(userDTO -> {
            GetActivityDetailResult.User user=new GetActivityDetailResult.User();
            user.setUserName(userDTO.getUserName());
            user.setUserId(userDTO.getUserId());
            user.setValid(userDTO.valid());
            return user;
        }).collect(Collectors.toList()));

    }

    private FindActivityListResult convertTOFindActivityResult(PageListDTO<ActivityDTO> activityList) {
        if(null==activityList){
            throw new ControllerException(ControllerErrorCodeConstants.ACTIVITY_LIST_IS_NULL);
        }
        FindActivityListResult result=new FindActivityListResult();
        result.setTotal(activityList.getTotal());
        result.setRecords(
                activityList.getRecords().stream().map(
                        activityDTO -> {
                            FindActivityListResult.ActivityInfo activityInfo = new FindActivityListResult.ActivityInfo();
                            activityInfo.setActivityId(activityDTO.getActivityId());
                            activityInfo.setActivityName(activityDTO.getActivityName());
                            activityInfo.setDescription(activityDTO.getDescription());
                            activityInfo.setValid(activityDTO.valid());
                            return activityInfo;
                        }).collect(Collectors.toList())
        );
        return result;
    }
    private CreateActivityResult convertTOCreateActivityResult(CreateActivityDTO createActivityDTO) {
        if(createActivityDTO==null){
            throw new ControllerException(ControllerErrorCodeConstants.CREATE_ACTIVITY_ERROR);
        }
        CreateActivityResult result=new CreateActivityResult();
        result.setActivityId(createActivityDTO.getActivityId());
        return result;
    }


}
